<?php
namespace app\customer\controller;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Banner extends Base{
    public function qiniuupload(){
        $sysres= Db::name('system')->where('id',1)->find();
        if ($sysres['file_state']==0) {
            $file=$_FILES['file'];
            $imgpath="../public/uploads/images/";
            $imgsubpath=date('Ymd',time())."/";
            if(!file_exists($imgpath)){
                mkdir($imgpath,0777);
            }
            if(!file_exists($imgpath.$imgsubpath)){
                mkdir($imgpath.$imgsubpath,0777);
            }
            chmod($imgpath.$imgsubpath,0777);
            move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
            return json(['code' => 200, 'data' =>config('tmdhttp').$_SERVER['HTTP_HOST']."/uploads/images/".$imgsubpath.$file['name'], 'msg' => '成功']);
        }elseif($sysres['file_state']==1){
            $auth = new Auth($sysres['accesskey'],$sysres['secretkey']);
            $bucket = $sysres['bucket'];
            $token = $auth->uploadToken($bucket);
            $filePath =$_FILES['file']['tmp_name'];
            $key = $_FILES['file']['name'];
            $uploadMgr = new UploadManager();
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            return json(['code' => 200, 'data' =>$sysres['url'].'/'.$_FILES['file']['name'], 'msg' => '成功']);
        }
    }
    public function banner_list(){
        $list=Db::name('banner')->where('cus_id',session('customer')['id'])->where('state','<',3)->order('id desc')->paginate(10);
        $lists=Db::name('banner')->where('cus_id',session('customer')['id'])->where('state','<',3)->select();
        $data=$list->all();
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function banner_add(){
        if(request()->isGet()){
            $mtype_cn = [0=>'首页轮播图',1=>'产品轮播图',2=>'案例轮播图',3=>'新闻轮播图',4=>'关于我们轮播图',5=>'联系我们轮播图',6=>'智能广告'];
            $this->assign('mtype_cn',$mtype_cn);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cus_id']=session('customer')['id'];
            $data['mtype']=$param['mtype']-1;
            $data['pcn']=$param['pcn'];
            $data['title']=$param['title'];
            $data['img']=$param['img'];
            $data['link']=$param['link'];
            $data['sort']=$param['sort'];
            $data['link_style']=$param['link_style'];
            $data['webpage']=$param['webpage'];
            $data['wxappid']=$param['wxappid'];
            $data['wxpage']=$param['wxpage'];
            $data['bdappid']=$param['bdappid'];
            $data['bdpage']=$param['bdpage'];
            $data['zjappid']=$param['zjappid'];
            $data['zjpage']=$param['zjpage'];
            $data['state']=1;
            $data['time']=time();
            $data['uptime']=time();
            $res=Db::name('banner')->insert($data);
            if($res){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function banner_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $mtype_cn = [0=>'首页轮播图',1=>'产品轮播图',2=>'案例轮播图',3=>'新闻轮播图',4=>'关于我们轮播图',5=>'联系我们轮播图',6=>'智能广告'];
            $data=Db::name('banner')->where('id',$param['id'])->find();
            $this->assign('mtype_cn',$mtype_cn);
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['mtype']=$param['mtype']-1;
            $data['pcn']=$param['pcn'];
            $data['title']=$param['title'];
            $data['img']=$param['img'];
            $data['link']=$param['link'];
            $data['link_style']=$param['link_style'];
            $data['webpage']=$param['webpage'];
            $data['wxappid']=$param['wxappid'];
            $data['wxpage']=$param['wxpage'];
            $data['bdappid']=$param['bdappid'];
            $data['bdpage']=$param['bdpage'];
            $data['zjappid']=$param['zjappid'];
            $data['zjpage']=$param['zjpage'];
            $data['sort']=$param['sort'];
            $data['state']=1;
            $data['uptime']=time();
            $res= Db::name('banner')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('banner')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('banner')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function banner_del(){
        $param = input('post.');
        $res= Db::name('banner')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function banner_dels(){
        $param = input('post.');
        $res= Db::name('banner')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }//压缩图片并上传
    public function uploads(){
        header("Content-type: image/jpeg");
        $files=$_FILES['file'];
        $file = $files['tmp_name']; 
        $percent = 1; //图片压缩比 
        list($width, $height) = getimagesize($file); //获取原图尺寸 
        //缩放尺寸 
        $newwidth = $width * $percent; 
        $newheight = $height * $percent; 
        if(getimagesize($file)[2]==2){
           $src_im = imagecreatefromjpeg($file);
        }elseif(getimagesize($file)[2]==3){
            $src_im = imagecreatefrompng($file);
        }
        $dst_im = imagecreatetruecolor($newwidth, $newheight); 
        imagecopyresampled($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        imagejpeg($dst_im,$imgpath.$imgsubpath.$files['name']); //输出压缩后的图片 
        imagedestroy($dst_im); 
        imagedestroy($src_im);
        return json(['code' => 200, 'data' =>"/uploads/images/".$imgsubpath.$files['name'], 'msg' => '成功']);
    }
}
